﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MG_POS_PC.DAL;
using MG_POS_PC.Model;
using System.Data;

namespace MG_POS_PC.BLL
{
    public class UserManage
    {
        private UserDao userDao = new UserDao();

        public UserManage() { }

        public bool Exists(int us_id)
        {
            return userDao.Exists(us_id);
        }

        public void Add(User user)
        {
            userDao.Add(user);
        }

        public bool Update(User user)
        {
            return userDao.Update(user);
        }

        public bool Delete(int us_id)
        {

            return userDao.Delete(us_id);
        }

        public User GetUser(int us_id)
        {

            return userDao.GetUser(us_id);
        }

        public DataSet GetList(string strWhere)
        {
            return userDao.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return userDao.GetList(Top, strWhere, filedOrder);
        }

        public List<User> GetUserList(string strWhere)
        {
            DataSet ds = userDao.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }

        public List<User> DataTableToList(DataTable dt)
        {
            List<User> modelList = new List<User>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                User model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new User();
                    if (dt.Rows[n]["us_id"].ToString() != "")
                    {
                        model.us_id = int.Parse(dt.Rows[n]["us_id"].ToString());
                    }
                    model.us_login_id = int.Parse(dt.Rows[n]["us_login_id"].ToString());
                    model.us_pwd = dt.Rows[n]["us_pwd"].ToString();
                    model.us_info = dt.Rows[n]["us_info"].ToString();
                    model.us_active = dt.Rows[n]["us_active"].ToString();
                    if (dt.Rows[n]["us_color"].ToString() != "")
                    {
                        model.us_color = int.Parse(dt.Rows[n]["us_color"].ToString());
                    }
                    model.us_manager = dt.Rows[n]["us_manager"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        public DataSet GetAllList()
        {
            return GetList("");
        }
    }
}
